/**
 * 
 */
package test.wordcount;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

/**
 * @author yaron
 *
 */
public class TextRecord implements Writable, DBWritable {

	String line;

	@Override
	public void write(PreparedStatement statement) throws SQLException {
		statement.setString(1, this.line);
	}

	@Override
	public void readFields(ResultSet resultSet) throws SQLException {
		this.line = resultSet.getString(1);
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// Write the length of the string, and then each character
		out.writeInt(this.line.length());
		//out.writeChars(this.line);
		for (int i = 0; i < line.length(); i++)
			out.writeChar(line.charAt(i));
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// Read the length of the string, and then read all characters
		int strLen = in.readInt();
		StringBuffer sb = new StringBuffer(strLen);
		for (int i = 0; i < strLen; i++) {
			sb.append(in.readChar());
		}
		this.line = sb.toString();
	}

	@Override
	public String toString() {
		return line;
	}


}
